package com.example.cs_jump.Fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cs_jump.R;
import com.example.cs_jump.Util.ManagerAdapter;
import com.google.android.material.snackbar.Snackbar;

import org.w3c.dom.Text;

import static android.content.Context.MODE_PRIVATE;

public class CollectFragment extends Fragment {
    CoordinatorLayout coordinatorLayout;
    RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.collect, container, false);

        coordinatorLayout = view.findViewById(R.id.coordinator_collect);
        recyclerView = view.findViewById(R.id.collect_recycler);
        setHasOptionsMenu(true);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        showList();
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_main, menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                Snackbar.make(coordinatorLayout, "搜索区分大小写,搜索不出尝试使用其他关键字", Snackbar.LENGTH_LONG).show();
                SearchView searchView = (SearchView) item.getActionView();
                searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String s) {
                        ManagerAdapter.getFilter(s);
                        return true;
                    }

                    @Override
                    public boolean onQueryTextChange(String s) {
                        ManagerAdapter.getFilter(s);
                        return true;
                    }
                });
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showList() {
        ManagerAdapter.setManagerOrAdapter(recyclerView,getActivity(),"collect");
    }
}
